from fastapi import APIRouter, Query, HTTPException
from orm.models import *
from pydantic import BaseModel, Field, field_validator
from tortoise.exceptions import DoesNotExist
from fastapi import status

api_department_query = APIRouter()


@api_department_query.get('/query/department_list',
                          status_code=status.HTTP_200_OK,
                          description="部门列表")
async def get_depart_info(department_name: str = Query(None,
                                                       description="部门名称")):
    if department_name:
        department = await Department.filter(depart_name=department_name
                                             ).first()
        if not department:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                                detail="部门:{}不存在".format(department_name))
        employee = await department.employee.all()
        return {
            "code": status.HTTP_200_OK,
            "message": "success",
            "data": {
                "department": department,
                "employee": employee
            }
        }
    else:
        #如果不传department_name，就获取所有部门信息
        all_department = await Department.all()
        return {
            "code": status.HTTP_200_OK,
            "message": "success",
            "data": all_department
        }
